//
//  ThumbData.m
//

#import "ThumbData.h"

#import "Controller.h"

@implementation ThumbData

//------------------------------------------------------------------------------
- (id)init
	: (NSImage *)image
	fullPath: (NSString *) path
	fileLength: (UInt64) length
	date: (NSDate*) date
	imageSize: (NSSize) imgSize
	thumbState:(int) tState
{
	self = [super init];
	if (nil != self)
	{
		if (nil != image)
		{
			thumbnail = image;
			[thumbnail retain];

			width = [thumbnail size].width;
			height = [thumbnail size].height;
		}

		fullPath = path;
		[fullPath retain];

		name = [fullPath lastPathComponent];
		[name retain];


		fileDate = date;
		[fileDate retain];

		exifDate = nil;

		imageSize = imgSize;
		fileLength = length;

		thumbState = tState;

		fileType = FILE_TYPE_UNKNOWN;
		mediaType = MEDIA_TYPE_UNKNOWN;
	}

	return self;
}

//------------------------------------------------------------------------------
- (id)initWithFilename: (NSString *) path
{
	self = [super init];
	if (nil != self)
	{
		fullPath = path;
		[fullPath retain];

		name = [fullPath lastPathComponent];
		[name retain];
		[self setMediaType: [Controller mediaTypeForFile: fullPath]];
	}

	return self;
}

//------------------------------------------------------------------------------
- (void) setFileDate:(NSDate*) date
{
	[fileDate release];
	fileDate = date;
	[fileDate retain];
}

//------------------------------------------------------------------------------
- (void) setExifDate:(NSDate*) date
{
	[exifDate release];
	exifDate = date;
	[exifDate retain];
}

//------------------------------------------------------------------------------
- (void) setImageSize: (NSSize) size
{
	imageSize = size;
}

//------------------------------------------------------------------------------
- (void) setThumbnail: (NSImage *) image
{
	if (nil != thumbnail)
	{
		[thumbnail release];
		thumbnail = nil;
		width = 0;
		height = 0;
	}

	if (nil != image)
	{
		thumbnail = image;
		[thumbnail retain];

		width = [thumbnail size].width;
		height = [thumbnail size].height;
	}
}

//------------------------------------------------------------------------------
- (void) setThumbstate:(int) state
{
	thumbState = state;
}

//------------------------------------------------------------------------------
- (void) calculateState
{
	if (nil != thumbnail)
	{
		BOOL thumbWidthBigger = width >= height;
		BOOL imageWidthBigger = imageSize.width >= imageSize.height;
		if (thumbWidthBigger != imageWidthBigger)
		{
			thumbState = THUMB_STATE_WRONG_EXIF;
		}
		else
		{
			thumbState = THUMB_STATE_VALID_EXIF;
		}
	}
	else
	{
		if (THUMB_STATE_VALID_EXIF == thumbState ||
			THUMB_STATE_WRONG_EXIF == thumbState)
		{
			thumbState = THUMB_STATE_NOT_IN_EXIF;
		}
	}
}

//------------------------------------------------------------------------------
- (BOOL)isEqual:(id)object
{
	return [fullPath isCaseInsensitiveLike: [object fullPath]];
}

//------------------------------------------------------------------------------
- (int) width
{
	return width;
}

//------------------------------------------------------------------------------
- (int) height
{
	return height;
}

//------------------------------------------------------------------------------
- (NSImage *) thumbnail
{
	return thumbnail;
}

//------------------------------------------------------------------------------
- (NSString *) fullPath
{
	return fullPath;
}

//------------------------------------------------------------------------------
- (NSString *) name
{
	return name;
}

//------------------------------------------------------------------------------
- (BOOL) doDatesMatch
{
	if (nil == exifDate)
	{
		return YES;
	}

	NSTimeInterval diff = [exifDate timeIntervalSinceDate: fileDate];
	return diff > -2.0 && diff < 2.0;
}

//------------------------------------------------------------------------------
- (NSDate *) date
{
	if (nil != exifDate)
	{
		return exifDate;
	}

	return fileDate;
}

//------------------------------------------------------------------------------
- (NSDate *) fileDate
{
	return fileDate;
}

//------------------------------------------------------------------------------
- (NSDate *) exifDate
{
	return exifDate;
}

//------------------------------------------------------------------------------
- (NSSize) imageSize
{
	return imageSize;
}

//------------------------------------------------------------------------------
- (uint64) fileLength
{
	return fileLength;
}

//------------------------------------------------------------------------------
- (int) thumbState
{
	return thumbState;
}

//------------------------------------------------------------------------------
- (int) fileType
{
	return fileType;
}

//------------------------------------------------------------------------------
- (void) setFileType:(int)type
{
	fileType = type;
}

//------------------------------------------------------------------------------
- (int) mediaType
{
	return mediaType;
}

//------------------------------------------------------------------------------
- (void) setMediaType:(int)type
{
	mediaType = type;
}

//------------------------------------------------------------------------------
- (void) dealloc
{
	[thumbnail release];
	[fullPath release];
	[name release];
	[fileDate release];
	[exifDate release];

	[super dealloc];
}

@end
